<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/6/22
 * Time: 18:35
 */

namespace Biz\tools;


class Link
{
    const LINK_H5_LOGIN = 'needLogin'; // 需要验证登陆的跳转链接

    const LINK_TYPE_NORMAL = 100;
    const LINK_TYPE_H5 = 1;
    const LINK_TYPE_TAG = 2;
    const LINK_TYPE_ARTICLE = 3;
    const LINK_TYPE_GUIDE = 4;
    const LINK_TYPE_MALL = 5;
    const LINK_TYPE_PHOTO_COLLECTION = 6;
    const LINK_PERSONAL_CENTER = 7;
    const LINK_SEARCH = 8;
    const LINK_TOPIC = 9;
    const LINK_INSPIRATION = 10;
    const LINK_SPECIAL = 11;
    const LINK_CUSTOMIZE = 12;

    public static $linkType = [
        self::LINK_TYPE_NORMAL           => '无链接',
        self::LINK_TYPE_H5               => 'H5页面',
        self::LINK_TYPE_TAG              => '标签相关页面',
        self::LINK_TYPE_ARTICLE          => '整屋案例',
        self::LINK_TYPE_GUIDE            => '居住指南',
        self::LINK_TYPE_MALL             => '商城相关页',
        self::LINK_TYPE_PHOTO_COLLECTION => '图片征集',
        self::LINK_PERSONAL_CENTER       => '个人中心',
        self::LINK_SEARCH                => '搜索',
        self::LINK_TOPIC                 => '话题',
        self::LINK_INSPIRATION           => '灵感集',
        self::LINK_SPECIAL               => '专题',
        self::LINK_CUSTOMIZE             => '自定义页面',
    ];

    const LINK_NORMAL = 100;
    const LINK_H5 = 1;
    const LINK_TAG_TOGETHER = 2;
    const LINK_ARTICLE_ADD = 3;
    const LINK_ARTICLE_SEARCH = 4;
    const LINK_GUIDE = 5;
    const LINK_ARTICLE = 6;
    const LINK_MALL_GOODS_ID = 7;
    const LINK_MALL_CATEGORY_GOODS_LIST = 8;
    const LINK_MALL_ACTIVITY_GOODS_LIST = 9;
    const LINK_PHOTO_COLLECTION = 10;
    const LINK_COUPON = 11;
    const LINK_SEARCH_ALL = 12;
    const LINK_MALL_TAB = 13;
    const LINK_SEARCH_TAG = 14;
    const LINK_MEAL_INFO = 15;
    const LINK_FLASH_SALE = 16;
    const LINK_MALL_ALL_CATEGORY = 17;
    const LINK_MALL_SHOP = 18;
    const LINK_TOPIC_DETAIL = 19;
    const LINK_INSPIRATION_ID = 20;
    const LINK_SPECIAL_ID = 21;
    const LINK_CUSTOMIZE_APP = 22;
    const LINK_CUSTOMIZE_APP_BASE = 23;
    const LINK_CUSTOMIZE_APP_V3 = 24;

    public static $link = [
        self::LINK_NORMAL                   => '无链接',
        self::LINK_H5                       => 'H5页面',
        self::LINK_TAG_TOGETHER             => '标签相关页面',
        self::LINK_ARTICLE_ADD              => '文章添加',
        self::LINK_ARTICLE_SEARCH           => '整屋频道搜索结果页',
        self::LINK_GUIDE                    => '居住指南详情页',
        self::LINK_ARTICLE                  => '整屋案例详情页',
        self::LINK_MALL_GOODS_ID            => '商品详情页',
        self::LINK_MALL_CATEGORY_GOODS_LIST => '二级分类列表页',
        self::LINK_MALL_ACTIVITY_GOODS_LIST => '商城活动页',
        self::LINK_PHOTO_COLLECTION         => '图片征集',
        self::LINK_COUPON                   => '我的优惠券',
        self::LINK_SEARCH_ALL               => '大搜索-各种tab',
        self::LINK_MALL_TAB                 => '商城tab',
        self::LINK_SEARCH_TAG               => '搜索图片',
        self::LINK_MEAL_INFO                => '套餐详情',
        self::LINK_FLASH_SALE               => '限时购频道页',
        self::LINK_MALL_ALL_CATEGORY        => '全部分类页',
        self::LINK_MALL_SHOP                => '店铺主页',
        self::LINK_TOPIC_DETAIL             => '话题详情页',
        self::LINK_INSPIRATION_ID           => '灵感集',
        self::LINK_SPECIAL_ID               => '专题详情页',
        self::LINK_CUSTOMIZE_APP            => 'APP内置页',
        self::LINK_CUSTOMIZE_APP_BASE       => 'APP内置页Base64',
        self::LINK_CUSTOMIZE_APP_V3         => 'APP内置页v3',
    ];

    const RELATION = [
        self::LINK_TYPE_NORMAL           => [self::LINK_NORMAL],
        self::LINK_TYPE_H5               => [self::LINK_H5],
        self::LINK_TYPE_TAG              => [self::LINK_TAG_TOGETHER],
        self::LINK_TYPE_ARTICLE          => [
            self::LINK_ARTICLE_ADD,
            self::LINK_ARTICLE_SEARCH,
            self::LINK_ARTICLE
        ],
        self::LINK_TYPE_GUIDE            => [self::LINK_GUIDE],
        self::LINK_TYPE_MALL             => [
            self::LINK_MALL_GOODS_ID,
            self::LINK_MALL_CATEGORY_GOODS_LIST,
            self::LINK_MALL_ACTIVITY_GOODS_LIST,
            self::LINK_MALL_TAB,
            self::LINK_MEAL_INFO,
            self::LINK_FLASH_SALE,
            self::LINK_MALL_ALL_CATEGORY,
            self::LINK_MALL_SHOP
        ],
        self::LINK_TYPE_PHOTO_COLLECTION => [self::LINK_PHOTO_COLLECTION],
        self::LINK_PERSONAL_CENTER       => [self::LINK_COUPON],
        self::LINK_SEARCH                => [
            self::LINK_SEARCH_ALL,
            self::LINK_SEARCH_TAG
        ],
        self::LINK_TOPIC                 => [
            self::LINK_TOPIC_DETAIL
        ],
        self::LINK_INSPIRATION           => [
            self::LINK_INSPIRATION_ID
        ],
        self::LINK_SPECIAL               => [
            self::LINK_SPECIAL_ID
        ],
        self::LINK_CUSTOMIZE             => [
            self::LINK_CUSTOMIZE_APP,
            self::LINK_CUSTOMIZE_APP_BASE,
            self::LINK_CUSTOMIZE_APP_V3
        ]
    ];

    public static $linkTypeArr = [
        self::LINK_H5                       => self::LINK_TYPE_H5,
        self::LINK_TAG_TOGETHER             => self::LINK_TYPE_TAG,
        self::LINK_ARTICLE_ADD              => self::LINK_TYPE_ARTICLE,
        self::LINK_ARTICLE_SEARCH           => self::LINK_TYPE_ARTICLE,
        self::LINK_GUIDE                    => self::LINK_TYPE_GUIDE,
        self::LINK_ARTICLE                  => self::LINK_TYPE_ARTICLE,
        self::LINK_MALL_GOODS_ID            => self::LINK_TYPE_MALL,
        self::LINK_MALL_CATEGORY_GOODS_LIST => self::LINK_TYPE_MALL,
        self::LINK_MALL_ACTIVITY_GOODS_LIST => self::LINK_TYPE_MALL,
        self::LINK_PHOTO_COLLECTION         => self::LINK_TYPE_PHOTO_COLLECTION,
        self::LINK_COUPON                   => self::LINK_PERSONAL_CENTER,
        self::LINK_SEARCH_ALL               => self::LINK_SEARCH,
        self::LINK_MALL_TAB                 => self::LINK_TYPE_MALL,
        self::LINK_SEARCH_TAG               => self::LINK_SEARCH,
        self::LINK_MEAL_INFO                => self::LINK_TYPE_MALL,
        self::LINK_FLASH_SALE               => self::LINK_TYPE_MALL,
        self::LINK_MALL_ALL_CATEGORY        => self::LINK_TYPE_MALL,
        self::LINK_MALL_SHOP                => self::LINK_TYPE_MALL,
        self::LINK_INSPIRATION_ID           => self::LINK_INSPIRATION,
        self::LINK_SPECIAL_ID               => self::LINK_SPECIAL,
        self::LINK_CUSTOMIZE_APP            => self::LINK_CUSTOMIZE,
        self::LINK_CUSTOMIZE_APP_BASE       => self::LINK_CUSTOMIZE,
        self::LINK_CUSTOMIZE_APP_V3         => self::LINK_CUSTOMIZE,
    ];

    public static $linkExample = [
        self::LINK_H5                       => [
            'param'       => 'url',
            'link_format' => 'http://www.haohaozhu.com',
        ],
        self::LINK_TAG_TOGETHER             => [
            'param'       => 'tag',
            'link_format' => '收纳',
        ],
        self::LINK_ARTICLE_ADD              => [
            'param'       => '',
            'link_format' => 'hhz://article-add',
        ],
        self::LINK_ARTICLE_SEARCH           => [
            'param'       => 'keyword',
            'link_format' => '{客厅}',
        ],
        self::LINK_GUIDE                    => [
            'param'       => 'guide_id',
            'link_format' => '00000v0000000987',
        ],
        self::LINK_ARTICLE                  => [
            'param'       => 'article_id',
            'link_format' => '00000v0000000986',
        ],
        self::LINK_MALL_GOODS_ID            => [
            'param'       => 'goods_id',
            'link_format' => '123',
        ],
        self::LINK_MALL_CATEGORY_GOODS_LIST => [
            'param'       => 'json',
            'link_format' => '{"category_id":1,"sub_category_id":23}',
        ],
        self::LINK_MALL_ACTIVITY_GOODS_LIST => [
            'param'       => 'json',
            'link_format' => '{"params":{"keyword": "0","tag": "0"},"need_sort":"0","title":""}',
        ],
        self::LINK_PHOTO_COLLECTION         => [
            'param'       => 'activity_id',
            'link_format' => '0000092030000981',
        ],
        self::LINK_COUPON                   => [
            'param'       => 'my_coupon_list',
            'link_format' => 'my_coupon_list'
        ],
        self::LINK_SEARCH_ALL               => [
            'param'       => 'common_search',
            'link_format' => '{"keyword":"tag","select_tab":"1","params":{"sort_type":"0","user_type":"1"}}'
        ],
        self::LINK_MALL_TAB                 => [
            'param'       => 'mall_tab',
            'link_format' => 'mall_tab'
        ],
        self::LINK_SEARCH_TAG               => [
            'param'       => 'search-tag',
            'link_format' => '卧室',
        ],
        self::LINK_MEAL_INFO                => [
            'param'       => 'meal_detail',
            'link_format' => '1',
        ],
        self::LINK_FLASH_SALE               => [
            'param'       => 'flash_sale',
            'link_format' => '{"tab":0或1,"hasNext":0或1}'
        ],
        self::LINK_MALL_ALL_CATEGORY        => [
            'param'       => '',
            'link_format' => 'mall_allcategory'
        ],
        self::LINK_MALL_SHOP                => [
            'param'       => '',
            'link_format' => 'user:0'
        ],
        self::LINK_TOPIC_DETAIL             => [
            'param'       => 'json',
            'link_format' => '{"topic_id":"2313","search_type":"1","sort_type":"1"}'
        ],
        self::LINK_INSPIRATION_ID           => [
            'param'       => '灵感集ID',
            'link_format' => '2470590'
        ],
        self::LINK_SPECIAL_ID               => [
            'param'       => '话题ID',
            'link_format' => '000000302000097p'
        ],
        self::LINK_CUSTOMIZE_APP            => [
            'param'       => '自定义APP页面',
            'link_format' => '请输入APP链接'
        ],
        self::LINK_CUSTOMIZE_APP_BASE       => [
            'param'       => '自定义APP页面Base64',
            'link_format' => '请输入Base64后的APP链接'
        ],
        self::LINK_CUSTOMIZE_APP_V3         => [
            'param'       => '自定义APP页面V3',
            'link_format' => '请输入niceliving协议的链接'
        ],
    ];

    public static $linkArray = [
        self::LINK_NORMAL                   => '',
        self::LINK_H5                       => 'urlb',
        self::LINK_TAG_TOGETHER             => 'tagtogether_tag',
        self::LINK_ARTICLE_ADD              => 'article-add',
        self::LINK_ARTICLE_SEARCH           => 'article_search',
        self::LINK_GUIDE                    => 'guide',
        self::LINK_ARTICLE                  => 'article',
        self::LINK_MALL_GOODS_ID            => 'mall_goods_id',
        self::LINK_MALL_CATEGORY_GOODS_LIST => 'mall_category_goods_list',
        self::LINK_MALL_ACTIVITY_GOODS_LIST => 'mall_activity_goods_list',
        self::LINK_PHOTO_COLLECTION         => 'activity',
        self::LINK_COUPON                   => '',
        self::LINK_SEARCH_ALL               => 'common_search',
        self::LINK_MALL_TAB                 => '',
        self::LINK_SEARCH_TAG               => 'search-tag',
        self::LINK_MEAL_INFO                => 'meal_detail',
        self::LINK_FLASH_SALE               => 'flash_sale',
        self::LINK_MALL_SHOP                => '',
        self::LINK_MALL_ALL_CATEGORY        => '',
        self::LINK_TOPIC_DETAIL             => 'topic_detail',
        self::LINK_INSPIRATION_ID           => 'ideabook',
        self::LINK_SPECIAL_ID               => 'specialItem',
        self::LINK_CUSTOMIZE_APP            => '',
        self::LINK_CUSTOMIZE_APP_BASE       => '',
        self::LINK_CUSTOMIZE_APP_V3         => '',
    ];

    public static $linkParam = [];

    public static $link_prefix = 'hhz://';

    public static $link_v3_prefix = 'niceliving://';

    public static function format($linkId, $linkDest, $isBase64 = false, $isLogin = false)
    {
        if (!$linkId) return '';
        if ($isBase64 === true) {
            $linkDest = base64_encode(trim($linkDest));
        }

        $linkType = self::getLinkType($linkId);
        if($linkId==self::LINK_CUSTOMIZE_APP_V3){
            return self::$link_v3_prefix . $linkType . $linkDest;
        }else{
            if ($isLogin == true) {
                $link = base64_encode(self::$link_prefix . $linkType . $linkDest);
                return self::$link_prefix . self::LINK_H5_LOGIN . ':' . $link;
            } else {
                return self::$link_prefix . $linkType . $linkDest;
            }
        }
    }

    public static function getLinkById($linkId)
    {
        $result['link_id'] = $linkId;
        $result['link_name'] = self::$link[$linkId];
        $result['link_param'] = self::$linkArray[$linkId];
        return $result;
    }

    private static function getLinkType($linkId)
    {
        $linkType = self::$linkArray[$linkId];
        if (empty($linkType)) {
            $linkType = '';
        } else {
            $linkType = $linkType . ':';
        }
        return $linkType;
    }
}